package com.hoelg.hgc;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hoelg.hgc.DAO.HGCUser;
import com.hoelg.hgc.constants.Utils;
import com.hoelg.hgc.webservices.LnC;

public class MainActivity extends Activity implements OnClickListener {

	public Refresh task;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		initialyseUI();        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    

	public void initialyseUI(){
        setContentView(R.layout.activity_main);
		Button button = (Button)findViewById(R.id.btn_refresh);
		button.setOnClickListener(this);
		TextView solde = (TextView) MainActivity.this.findViewById(R.id.tvSolde);
		solde.setText(HGCUser.getInstance().getBalance() + "€");
	}
	
	

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_refresh:
			String mntRetrait = ((EditText)findViewById(R.id.etret)).getText().toString();
			if (!Utils.isNullOrEmpty(mntRetrait)) {
				task = new Refresh();
				task.execute(mntRetrait);
			}
			String mntDepense = ((EditText)findViewById(R.id.etdep)).getText().toString();
			if (!Utils.isNullOrEmpty(mntDepense)) {
				task = new Refresh();
				task.execute("-"+mntDepense);
			}

		default:
			break;
		}
	}
	
	
	
	@Override
	protected Dialog onCreateDialog(int id) {
		ProgressDialog dialog = new ProgressDialog(MainActivity.this);
		dialog.setCancelable(true);
		dialog.setMessage("Calcul en cours...");
		// set the progress to be horizontal
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		return dialog;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		initialyseUI();
		removeDialog(0);
	}

	//classe interne de gestion des taches de fond de la connexion
	public class Refresh extends AsyncTask<String, Void, Void> {

		private boolean allOk = false;
				
		@Override
		protected void onPreExecute() {
			//gestion de l'attente
			showDialog(0);
		}

		@Override
		protected void onPostExecute(final Void unused) {
			removeDialog(0);
			//connect();
			if (allOk){
				TextView solde = (TextView) MainActivity.this.findViewById(R.id.tvSolde);
				solde.setText(HGCUser.getInstance().getBalance() + "€");
			}
			else{
				Toast toast = Toast.makeText(getApplicationContext(), "Problème lors du calcul du solde", Toast.LENGTH_SHORT);
				toast.show();
				return;
			}
			((EditText)MainActivity.this.findViewById(R.id.etdep)).setText("");
			((EditText)MainActivity.this.findViewById(R.id.etret)).setText("");
		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				LnC ws = new LnC(Double.parseDouble(params[0]));

				ws.executeWebService();

				if (Utils.isNullOrEmpty(ws.getResultatWS()))
					allOk = false;
				else
					allOk = ws.getResultatWS().equals("OK");
				//String result = ws.getResultatWS();
				
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// TODO Auto-generated method stub
			return null;
		}

	}
}
